(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.apProgram.updateVerifyController', updateVerifyController);
	updateVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'apProgramService', '$filter' ];
	function updateVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, apProgramService, $filter) {
		var ctrl = this, apProgramInfo = $scope.$stateParams.params, errorMessage = {};
		//ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.apProgram = {};
		
		ctrl.init = function() {
			var params = {
					taskId : apProgramInfo.taskId
			};
			// init acct
			apProgramService.initProfAcct(layoutConfig, $scope, ctrl, true);
			// init int
			//apProgramService.initProfInt(layoutConfig, $scope, ctrl, true);
			//apProgramService.initProfCostOfFundInt(layoutConfig, $scope, ctrl, true);
			// init pastdue
			apProgramService.initProfPastdue(layoutConfig, $scope, ctrl, true);
			// init fee
			apProgramService.initProfFee(layoutConfig, $scope, ctrl, true);
			// init doc
			apProgramService.initEventDoccfg(layoutConfig, $scope, ctrl, true);
			// init limit facility
			//apProgramService.initProfLimitFaci(layoutConfig, $scope, ctrl, true);
			//apProgramService.initProfLimitInsFaci(layoutConfig, $scope, ctrl, true);
			//apProgramService.initProfLimitPurFaci(layoutConfig, $scope, ctrl, true);
			// init Settlement Instruction
			apProgramService.initProfSetlinst(layoutConfig, $scope, ctrl, true);
			// init cust prop define
			apProgramService.initCustProp(layoutConfig, $scope, ctrl, true);
			//
			apProgramService.initGeneral($scope, ctrl, true).then(function() {
				authService.updateVerify(params).then(function(response) {
					var initApProgram = response.data.model.newModel;
					ctrl.action = "updateVerify";
					ctrl.apProgramOld = response.data.model.oldModel || {};
					apProgramService.initLayoutConfigAndModel($scope, ctrl, initApProgram, ctrl.action, false).then(function() {
						// ctrl.apProgram = initApProgram; done in initLayoutConfig
						ctrl.apProgram.taskId = response.data.task.taskId || apProgramInfo.taskId;
						//
						ctrl.apProgramOld.profAccts = ctrl.apProgramOld.profAccts || [];
						ctrl.apProgramOld.profPastdues = ctrl.apProgramOld.profPastdues || [];
						ctrl.apProgramOld.profFees = ctrl.apProgramOld.profFees || [];
						ctrl.apProgramOld.eventDoccfgs = ctrl.apProgramOld.eventDoccfgs || [];
						ctrl.apProgramOld.profSetlinsts = ctrl.apProgramOld.profSetlinsts || [];
					});
				});
			});
		};

		ctrl.updateVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [$filter("encode")(ctrl.apProgram.buyerCode)]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateVerifyApprove(ctrl.apProgram).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		ctrl.updateVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.apProgram.comments = comments;
						authService.updateVerifyReject(ctrl.apProgram).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.approgram.list',
					'state' : 'cda.apProgram.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.approgram.list',
					'state' : 'cda.apProgram.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);